package com.woniuxy.amusementparkos.permission.controller;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.amusementparkos.dto.lvd.TableDataDto;
import com.woniuxy.amusementparkos.entity.fzw.Menu;
import com.woniuxy.amusementparkos.entity.fzw.Organization;
import com.woniuxy.amusementparkos.entity.fzw.Role;
import com.woniuxy.amusementparkos.exception.fzw.OrganizationException;
import com.woniuxy.amusementparkos.exception.fzw.OrganizationInUseException;
import com.woniuxy.amusementparkos.exception.fzw.RoleException;
import com.woniuxy.amusementparkos.exception.fzw.RoleInUseException;
import com.woniuxy.amusementparkos.service.fzw.OrganizationService;
import com.woniuxy.amusementparkos.util.lzw.result.ResponseResult;
/**
 * 组织表查询
 * @author fzw
 *
 */
@Controller
public class OrganizationController {
	@Autowired
	OrganizationService organizationService;
	
	/**
	 * 根据请求查询组织名
	 * @return 返回json类型的组织名
	 */
	@RequestMapping("/organization/getOne.do")
	@ResponseBody
	public Organization getOrganizationName(int organization_id) {
		Organization organization;
		organization = organizationService.selectOrganizationNameById(organization_id);
		return organization;
	}
	/**
	 * 根据请求传回查询所有的组织名
	 * @return
	 */
	@RequestMapping("/organization/getAll.do")
	@ResponseBody
	public List<Organization> getAllOrganizationName() {
		List<Organization> organizations;
		organizations = organizationService.selectAllOrganizationNames();
		return organizations;
	}
	
	/**
	 * 获取一页的组织数据
	 * @param page
	 * @param limit
	 * @return
	 */
	@RequestMapping("/organizationMg/getAllOrganization.do")
	@ResponseBody
	public TableDataDto<Organization> getAllOrganization(@Param("page")Integer page,@Param("limit")Integer limit){
		TableDataDto<Organization> res = new TableDataDto<Organization>();
		try {
			//查询一页的角色信息
			List<Organization> organizations=organizationService.selectAllOrganizationByPageNumber(page,limit);
			//查询总条数
			Long count=organizationService.selectCount();
			res.setCount(count);
			res.setData(organizations);
			res.setCode(200);
			res.setMsg("获取组织数据成功");
			return res;
		} catch (RoleException e) {
			res.setMsg("组织信息处理异常，请联系管理员！");
			res.setCode(4);
			return res;
		}catch (Exception e) {
			res.setMsg("系统处理异常！");
			res.setCode(4);
			return res;
		}
	}
	
	@RequestMapping("/organizationMg/insertOrganization")
	@ResponseBody
	public ResponseResult<Organization> insertOrganization(Organization organization){
		ResponseResult<Organization> res = new ResponseResult<Organization>(404,"服务器错误",null);
		try {
			//插入角色信息
			organizationService.insertOrganization(organization);
			res.setCode(200);
			res.setMsg("添加成功！");
			return res;
		} catch (RoleException e) {
			res.setMsg("组织添加异常！");
			res.setCode(4);
			return res;
		}catch (Exception e) {
			res.setMsg("系统处理异常！");
			res.setCode(4);
			return res;
		}
	}
	@RequestMapping("/organizationMg/deleteOrganizations")
	@ResponseBody
	public ResponseResult<Organization> deleteRoles(int[] organization_ids){
		ResponseResult<Organization> res = new ResponseResult<Organization>(404,"服务器错误",null);
		try {
			//删除角色信息前先检验该角色是否正被用户使用，即被从表关联
			organizationService.selectOrganizationByOrganizationIds(organization_ids);
			//删除角色信息
			organizationService.deleteOrganizations(organization_ids);
			res.setCode(200);
			res.setMsg("删除成功！");
			return res;
		}catch (OrganizationInUseException e) {
			res.setMsg(e.getMessage());
			res.setCode(4);
			return res;
		} 
		catch (OrganizationException e) {
			res.setMsg("角色删除异常！");
			res.setCode(4);
			return res;
		}catch (Exception e) {
			res.setMsg("系统处理异常！");
			res.setCode(4);
			return res;
		}
	}
	@RequestMapping("/organizationMg/updateOne")
	@ResponseBody
	public ResponseResult<Organization> updateOrganization(Organization organization){
		ResponseResult<Organization> res = new ResponseResult<Organization>(404,"服务器错误",null);
		try {
			//插入角色信息
			organizationService.updateOrganization(organization);
			res.setCode(200);
			res.setMsg("添加成功！");
			return res;
		} catch (RoleException e) {
			res.setMsg("组织修改异常！");
			res.setCode(4);
			return res;
		}catch (Exception e) {
			res.setMsg("系统处理异常！");
			res.setCode(4);
			return res;
		}
	}
}
